﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entity;
using BUS;
namespace CustomerManager
{
    public partial class ucDebt : UserControl
    {
        public FrmMain frm;
        int status;
        DataTable dtCaNhan;
        DataTable dtDoanhNghiep;
        DataTable dtKhoanVayDenHan;
        String filterNV = "";
        public ucDebt()
        {
            InitializeComponent();
        }
        public void loadfrmmain()
        {
            frm = (FrmMain)Application.OpenForms["FrmMain"];
        }
        public void LoadCustomer(int s)
        {
            status = s;

            if (status == -1)
            {
                dtCaNhan = PersonalBUS.LayTatCaKHCaNhan();
                dgvCaNhan.DataSource = dtCaNhan;
                dtDoanhNghiep = CompanyBUS.LayTatCaKHDoanhNghiep();
                dgvDoanhNghiep.DataSource = dtDoanhNghiep;

                dtKhoanVayDenHan = DebtBUS.LayKhoanVayDenHan();
                dgvkhoanvaydenhan.DataSource = dtKhoanVayDenHan;

                DataTable dt = NhanVienBUS.LayNhanVienTheoID(status);
                DataRow dr = dt.NewRow();
                dr["Tennv"] = "Tất cả Nhân Viên";
                dr["Manv"] = -1;
                dt.Rows.Add(dr);
                cmbNhanVien.DataSource = dt;
                cmbNhanVien.DisplayMember = "Tennv";
                cmbNhanVien.ValueMember = "Manv";

            }
            else
            {
                cmbNhanVien.Enabled = false;
                cmbNhanVien.DataSource = NhanVienBUS.LayNhanVienTheoID(status);
                cmbNhanVien.DisplayMember = "Tennv";
                dgvCaNhan.DataSource = PersonalBUS.LayKHCaNhanTheoNhanVien(status);
                dgvDoanhNghiep.DataSource = CompanyBUS.LayKHDoanhNghiepTheoNhanVien(status);
                dgvkhoanvaydenhan.DataSource = DebtBUS.LayKhoanVayDenHanTheoMaNV(status);
            }

            dgvCaNhan.BringToFront();
            dgvCaNhan.BringToFront();
            comboBox1.SelectedIndex = 0;
            cmbTieuChiCaNhan.SelectedIndex = 0;
            cmbNhanVien.SelectedIndex = cmbNhanVien.Items.Count - 1;
            cmbNhanVien_SelectedIndexChanged(new object(), new EventArgs());
            cmbTieuChiCaNhan_SelectedIndexChanged(new object(), new EventArgs());

        }

    
        
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == 0)
            {
                dgvCaNhan.BringToFront();
            }
            else
                dgvDoanhNghiep.BringToFront();
        }
        
        private void cmbNhanVien_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (cmbNhanVien.SelectedIndex == cmbNhanVien.Items.Count - 1)
                    filterNV = "";
                else
                    filterNV = "MaNhanVien = " + cmbNhanVien.SelectedValue;
                dtCaNhan.DefaultView.RowFilter = filterNV;
                dtDoanhNghiep.DefaultView.RowFilter = filterNV;
                dtKhoanVayDenHan.DefaultView.RowFilter = filterNV;
                txtTimKiem.Clear();
            }
            catch { }
        }

        string filterTCCN="";
        string filterTCDN="";
        string and = " and ";
        private void cmbTieuChiCaNhan_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (cmbTieuChiCaNhan.SelectedIndex == 0)
                {
                    filterTCCN = "Makhcanhan like '%";
                    filterTCDN = "Makhdoanhnghiep like '%";
                }
                else if (cmbTieuChiCaNhan.SelectedIndex == 1)
                {
                    filterTCCN = "Tenkhcanhan like '%";
                    filterTCDN = "Tendoanhnghiep like '%";
                }
                else
                {
                    filterTCCN = filterTCDN = "masothue like '%";
                }
            }
            catch { }
        }

        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == 0)
            {
                dtDoanhNghiep.DefaultView.RowFilter = filterNV;
                if (filterNV == "")
                    dtCaNhan.DefaultView.RowFilter = filterTCCN + txtTimKiem.Text + "%'";
                else
                    dtCaNhan.DefaultView.RowFilter = filterNV + and + filterTCCN + txtTimKiem.Text + "%'";
            }
            else
            {
                if (filterNV == "")
                    dtDoanhNghiep.DefaultView.RowFilter = filterTCDN + txtTimKiem.Text + "%'";
                else
                    dtDoanhNghiep.DefaultView.RowFilter = filterNV + and + filterTCDN + txtTimKiem.Text + "%'";
                dtCaNhan.DefaultView.RowFilter = filterNV;
            }
        }

        private void ucDebt_Load(object sender, EventArgs e)
        {
        }

        private void btnxemchitiet_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == 0)
            {
                if (dgvCaNhan.CurrentRow == null)
                    return;
                string MaKHCN = dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colMa"].Value.ToString();
                ucDebt_Personal frmPersonal = new ucDebt_Personal();
                frmPersonal.tenkh = dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colten"].Value.ToString();
                frmPersonal.LoadKhoanVay(0, MaKHCN);
                frm.Controls.Add(frmPersonal);
                frmPersonal.Dock = DockStyle.Fill;
                frmPersonal.BringToFront();
            }
            else
            {
                if (dgvDoanhNghiep.CurrentRow == null)
                    return;
                string MaKHDN = dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colMaDoanhNghiep"].Value.ToString();
                ucDebt_Company ucdeptcompany = new ucDebt_Company();
                ucdeptcompany.tenkhdn = dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colTenDoanhNghiep"].Value.ToString();
                ucdeptcompany.LoadKhoanVay(1, MaKHDN);
                frm.Controls.Add(ucdeptcompany);
                ucdeptcompany.Dock = DockStyle.Fill;
                ucdeptcompany.BringToFront();
            }
        }

        private void btnkhoanvaydenhan_Click(object sender, EventArgs e)
        {
  //          dgvkhoanvaydenhan.DataSource = BUS.DebtBUS.LayKhoanVayDenHan();
            dgvkhoanvaydenhan.BringToFront();
        }
    }
}
